package myStruct

import (
	"encoding/json"
	"fmt"
)



func init()  {
	fmt.Println("myStruct:--------------------------")

	type NewInt int

	type MyInt  = int

	var a NewInt
	var b MyInt
	fmt.Printf("type of a:%T\n",a)
	fmt.Printf("type of b:%T\n",b)

	type person struct {
		name string
		city string
		age int8
	}
	var p1 person
	p1.name="pp.cn"
	p1.city="Beijing"
	p1.age=18
	fmt.Println(p1)

	var p2 = new(person)
	fmt.Printf("%T\n",p2)

	p3:=&person{}
	p3.name="博客"
	(*p3).name="真正的博客"
	p3.age=30
	p3.city="成都"

	p5:=person{
		name:"pp.cnn",
		city:"Beijing",
		age: 120,
	}
	fmt.Println(p5)

	type student struct {
		name string
		age  int
	}

	m := make(map[string]*student)
	stus := []student{
		{name: "pprof.cn", age: 18},
		{name: "测试", age: 23},
		{name: "博客", age: 28},
	}

	for _, stu := range stus {
		m[stu.name] = &stu
	}
	for k, v := range m {
		fmt.Println(k, "=>", v.name)
	}
	//继承
	d1 := &Dog{
		Feet: 4,
		Animal: &Animal{ //注意嵌套的是结构体指针
			name: "乐乐",
		},
	}
	d1.wang() //乐乐会汪汪汪~
	d1.move() //乐乐会动！



	//json序列化
	type Student struct {
		ID     int
		Gender string
		Name   string
	}
	type Class struct {
		Title    string
		Students []*Student
	}
	c := &Class{
		Title:    "101",
		Students: make([]*Student, 0, 200),
	}
	for i := 0; i < 10; i++ {
		stu := &Student{
			Name:   fmt.Sprintf("stu%02d", i),
			Gender: "男",
			ID:     i,
		}
		c.Students = append(c.Students, stu)
	}

	data,err:=json.Marshal(c)
	if err != nil {
		fmt.Println("json marshal failed")
		return
	}
	fmt.Println(data)
	str := `{"Title":"101","Students":[{"ID":0,"Gender":"男","Name":"stu00"},{"ID":1,"Gender":"男","Name":"stu01"},{"ID":2,"Gender":"男","Name":"stu02"},{"ID":3,"Gender":"男","Name":"stu03"},{"ID":4,"Gender":"男","Name":"stu04"},{"ID":5,"Gender":"男","Name":"stu05"},{"ID":6,"Gender":"男","Name":"stu06"},{"ID":7,"Gender":"男","Name":"stu07"},{"ID":8,"Gender":"男","Name":"stu08"},{"ID":9,"Gender":"男","Name":"stu09"}]}`
	c1 := &Class{}
	err = json.Unmarshal([]byte(str), c1)
	if err != nil {
		fmt.Println("json unmarshal failed!")
		return
	}
	fmt.Printf("%#v\n", c1)

	fmt.Println("-------------------------")
}

type Animal struct {
	name string
}
func (a *Animal) move() {
	fmt.Printf("%s会动！\n", a.name)
}
//Dog 狗
type Dog struct {
	Feet    int8
	*Animal //通过嵌套匿名结构体实现继承
}
func (d *Dog) wang() {
	fmt.Printf("%s会汪汪汪~\n", d.name)
}
